package cn.zhaoyuening.hadoopdemo.mapper;

import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.log4j.Logger;

import java.io.IOException;
import java.util.Arrays;

public class MaxTemperatureMapper extends Mapper<LongWritable, Text, Text, DoubleWritable> {
    private static Logger logger = Logger.getLogger(MaxTemperatureMapper.class);

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        if (value.toString().length()>1&&value.toString().charAt(0)=='S'){
            return;
        }
//        logger.info("text = "+value.toString());
        //        将温度数据提取出来
        String[] weatherData = value.toString().split("\\s+");
//        logger.info(Arrays.toString(weatherData));
//        抛弃格式不一致数据
        if (weatherData.length != 22) {
            return ;
        }
//        提取时间
        String date = weatherData[2];
//        提取最高温度
        double maxTemperature = getFormatTemperature(weatherData[17]);

//        排除缺省值
//        if (maxTemperature>200){
//            return;
//        }
//        logger.info(date+"最高温度"+maxTemperature);
        context.write(new Text(date.trim()),new DoubleWritable(maxTemperature));
    }



    public float getFormatTemperature(String strVal){
        if (strVal.charAt(strVal.length()-1)=='*'){
            strVal = strVal.substring(0,strVal.length()-1);
        }
        return Float.valueOf(strVal);
    }
}
